package com.zmsoft.boxy.sfg.service;

import java.util.List;

import org.cnsoft.framework.beans.common.RESTResultBean;
import org.cnsoft.framework.utils.EmptyHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.zmsoft.boxy.sfg.player.BoxySFGPlayer;
import com.zmsoft.boxy.sfg.player.SfgPlayerFriendDBO;
import com.zmsoft.persistent.sfg.Sfg661010PlayerFarm.Sfg661010PlayerFarmDBO;
import com.zmsoft.persistent.sfg.Sfg661021PlayerLand.Sfg661021PlayerLandDBO;

/**
 * 超管登录
 * 
 * @author ZmSoft
 * @version 0.1.0 2018/3/1
 * @since 0.1.0 2018/3/1
 * @see ApiManagerLoginController
 */
@Service("SFGPlayerInfoService") // 替换系统登录接口
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
//@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SFGPlayerInfoService extends MySFGGameServiceSupport {

	/**
	 * 玩家农场信息
	 */
	public RESTResultBean<BoxySFGPlayer> loadPlayerFarm(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		// 检查用户数据是否存在
//		Sfg661010PlayerFarmDBO playerFarm = new Sfg661010PlayerFarmDBO();
//		param.setId(player.getUserId());
//		param.setPlayerId(player.getUserId());
		Sfg661010PlayerFarmDBO playerFarm = sfgPlayerInfoCacheService.loadPlayerFarm(player);

		// 不存在的场合创建数据
		if (EmptyHelper.isEmpty(playerFarm)) {
			// 第一次的场合
			playerFarm = sfgPlayerInfoCacheService.newPlayerFarm(player);
			// 返回信息提示
			result.setCode(2);
			result.setMsg("第一次进来，送你点种子，快去种植玩玩吧。");
		} else {
			result.setMsg("您终于回来啦，赶快看看有哪些变化吧......");
			//更新土地状态
			preparePlayerLand(player);
		}

		result.setData(playerFarm);
		return result;
	}

	/**
	 * 玩家土地信息
	 */
	public RESTResultBean<BoxySFGPlayer> loadPlayerLands(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		List<Sfg661021PlayerLandDBO> lands = sfgPlayerInfoCacheService.loadPlayerLands(player);
		preparePlayerLand(player, lands);
		result.setData(lands);
		return result;
	}

	/**
	 * 玩家仓库信息
	 */
	public RESTResultBean<BoxySFGPlayer> loadPlayerWarehouse(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		result.setData(sfgPlayerInfoCacheService.loadPlayerWarehouse(player));
		return result;
	}

	/**
	 * 玩家口袋信息
	 */
	public RESTResultBean<BoxySFGPlayer> loadPlayerPocket(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		result.setData(sfgPlayerInfoCacheService.loadPlayerPocket(player));
		return result;
	}

	/**
	 * 好友列表
	 * 
	 * @param init
	 * @return
	 * @throws Exception 
	 */
	public RESTResultBean<BoxySFGPlayer> loadPlayerFriend(BoxySFGPlayer player) throws Exception {
		RESTResultBean<BoxySFGPlayer> result = new RESTResultBean<BoxySFGPlayer>();
		List<SfgPlayerFriendDBO> friends = sfgPlayerInfoCacheService.loadPlayerFriend(player);
		result.setData(friends);
		prepareFriendLand(player,friends);
		return result;
	}

	///////////////////////////////////////////////////////////////////////////
	//@Async("threadPoolTaskExecutor")
	public void preparePlayerLand(BoxySFGPlayer player,List<Sfg661021PlayerLandDBO> lands) throws Exception {
		for (Sfg661021PlayerLandDBO land : lands) {
			SFGGameThrusterServiceHelper.preparePlayerLand(sfgGameInfoCacheService, sfgPlayerInfoCacheService, player, land);
		}
	}
	
	@Async("threadPoolTaskExecutor")
	public void preparePlayerLand(BoxySFGPlayer player) throws Exception {
		SFGGameThrusterServiceHelper.preparePlayerLand(sfgGameInfoCacheService, sfgPlayerInfoCacheService, player);
	}
	
	/**
	 * 异步更新好友土地里面的数据信息
	 * @param playerId
	 * @param friends
	 * @throws Exception 
	 */
	@Async("threadPoolTaskExecutor")
	public void prepareFriendLand(BoxySFGPlayer player, List<SfgPlayerFriendDBO> friends) throws Exception {
		BoxySFGPlayer frinedPlayer = new BoxySFGPlayer();
		for (SfgPlayerFriendDBO friend : friends) {
			//确认好友ID
			frinedPlayer.setUserId(friend.getPlayerFriendId());
			SFGGameThrusterServiceHelper.preparePlayerLand(sfgGameInfoCacheService, sfgPlayerInfoCacheService, frinedPlayer);
		}
	}
}
